// pages/me/binding/binding.js
const app = getApp()
import { sendCode, register, getuserInfo } from '../../../api/api'
Page({
  data: {
    height: app.globalData.height * 2 + 40,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '登录 ', //导航栏 中间的标题
      img: "../../image/index.png"
    },
    sendTime: '获取验证码',
    snsMsgWait: 60,
    smsFlag: false,
    tel: '',
    code: '',
    isShow: true,
    url: "/image/ty2.png",
    nickname: '',
    avatar: '',
    openid: '',
    isRegister: false
  },

  close_tap: function () {
    this.setData({
      isShow: true
    })
  },
  open_tap: function () {
    this.setData({
      isShow: false
    })
  },
  
  inputTel (e) {
    this.setData({ tel: e.detail.value })
  },

  inputCode (e) {
    this.setData({ code: e.detail.value })
  },

  sendCode(){
    if (this.data.smsFlag) return;
    this.setData({  smsFlag: true });
    // console.log(this.data.tel);
    if (this.data.tel === '') {
      this.setData({  smsFlag: false });
      app.showToast('手机号不能为空');
      return;
    }
    if (!(/^1[3|4|5|8][0-9]\d{8}$/.test(this.data.tel))) {
      this.setData({  smsFlag: false });
      app.showToast('手机号格式不对');
      return;
    }
    app.showLoading('请求中');
    // 请求 发送验证码
    const that = this;
    sendCode({mobile: this.data.tel}).then(res => {
      if (res.code === 0) {
        app.hideLoading();
        app.showToast('短信验证码发送成功，请注意查收','none');
        that.runTime();
      } else {
        this.setData({  smsFlag: false });
        app.hideLoading();
        app.showToast(res.msg);
      }
    })
  },

  // 倒计时 
  runTime () {
    var inter = setInterval(function() {
      this.setData({
        sendTime: this.data.snsMsgWait + 's后重发',
        snsMsgWait: this.data.snsMsgWait - 1
      });
      if (this.data.snsMsgWait < 0) {
        clearInterval(inter)
        this.setData({
          sendTime: '获取验证码',
          snsMsgWait: 60,
          smsFlag: false
        });
      }
    }.bind(this), 1000);
  },

  register () {
    if (!this.data.isShow) {
      app.showToast('请勾选用户协议')
      return;
    }
    if (this.data.isRegister) return;
    this.setData({  isRegister: true });
    const { tel, code , avatar, nickname, openid } = this.data;
    if (tel.length == '') {
      this.setData({  isRegister: false });
      app.showToast('手机号不能为空')
      return 
    }
    if (!(/^1[3|4|5|8][0-9]\d{8}$/.test(this.data.tel))) {
      this.setData({  isRegister: false });
      app.showToast('手机号格式不对');
      return;
    }
    if (code.length != 4) {
      this.setData({  isRegister: false });
      app.showToast('验证码格式不对')
      return 
    }
    const data = {
      openid,
      mobile: tel,
      captcha: code,
      avatar,
      nickname
    }
    // console.log(data);
    app.showLoading();
    register(data).then(res => {
      app.hideLoading();
      // console.log(res);
      if (res.code === 0 ) {
        // 注册成功 返回首页
        app.globalData.userId = res.data
        getuserInfo({openid}).then(user => {
          if (user.code == 0) {
            app.globalData.userId = res.data;
            app.globalData.hasMobile = true;
            app.globalData.userInfo = user.data;
            wx.navigateBack({delta: 2 });
          }
        })
      } else {
        this.setData({  isRegister: false });
        app.showToast(res.msg);
      }
    })
  },

  onShow () {
    this.setData({
      openid: app.globalData.openid,
      avatar: app.globalData.userInfo.avatarUrl,
      nickname: app.globalData.userInfo.nickName,
    })
  }
})